<template>
  <base-breadcrumb>
    <searchData ref="searchData" :search-data="searchArr" @search="search" />
    <a-card :bordered="false">
      <a-table ref="table" :scroll="{ x: 1376 }" :loading="loading" :pagination="false" :columns="columns" :data-source="tableData" row-key="id">
        <div slot="operation" slot-scope="text, record" class="pub-table-list-action">
          <template>
            <span v-btnPermission="'crm_002_loo_01'" @click="handleEdit(record, 3)">查看</span>
          </template>
        </div>
      </a-table>

      <pagination v-model="pageNo" :total="totalSize" :page-size.sync="pageSize" @change="handlePageChange" @showSizeChange="showSizeChange" />
    </a-card>
  </base-breadcrumb>
</template>
<script>
import { getMaintenanceInfo } from '@/api/complaint/maintenance'
// import SelectSubCompany from '@/components/Select/SelectSubCompany'
// import SelectProject from '@/components/Select/SelectProject' // 项目
import { queryScoreList } from '@/api/complaint/maintenancelog'
import SelectCompanyAndProject from '@/components/Select/SelectCompanyAndProject' // 组织
import OrgTreeSelect from '@/components/OrgTreeSelect'
export default {
  name: 'a' + Date.now(),
  data: function () {
    return {
      pathName: this.$route.path,
      loading: false,
      infoShow: false,
      queryParam: {},
      infoQueryParam: {},
      pageNo: 1,
      pageSize: 10,
      totalSize: 0,
      searchArr: [
          {
            name: '所属单位',
            valueKey: 'searchQuery',
            type: OrgTreeSelect,
          },
        // {
        //   name: '所属组织',
        //   valueKey: 'statisticsId',
        //   type: SelectCompanyAndProject
        // },
        // {
        //   name: '所属分公司',
        //   valueKey: 'branchComId',
        //   type: SelectSubCompany
        // },
        // {
        //     name: '项目名称',
        //     type: SelectProject,
        //     keyValue: 'branchComId',
        //     valueKey: 'projectId'
        // },
        {
          name: '维保单编号',
          value: 'code',
          type: 'input'
        },
        {
          name: '维保发起时间',
          value: 'createTime',
          type: 'dateRange',
          attr: {
            rangeFormat: ['startTime', 'endTime'],
          }
        },
        {
          name: '维保事项',
          value: 'infoId',
          type: 'select',
          data: []
        },
        {
          name: '得分情况',
          type: 'rangeInput',
          value: '',
          rangeKey: ['lowScore', 'upScore'],
          attr: {
            placeholderMin: '最小分数',
            placeholderMax: '最大分数',
            max: 100,
            min: 0
          }
        }
      ],
      columns: [
        {
          title: '维保单编号',
          dataIndex: 'code',
          width: 160
        },
        {
          title: '所属分公司',
          dataIndex: 'branchComName',
          width: 160
        },
        {
          title: '项目名称',
          dataIndex: 'projectName',
          width: 220
        },
        {
          title: '维保发起时间',
          dataIndex: 'createTime',
          width: 160
        },
        {
          title: '维保事项',
          dataIndex: 'infoName',
          width: 160
        },
        {
          title: '得分情况',
          dataIndex: 'totalScore',
          width: 160
        },
        {
          title: '操作',
          dataIndex: 'operation',
          width: 200,
          fixed: 'right',
          scopedSlots: { customRender: 'operation' }
        }
      ],
      selectedRowKeys: [],
      selectId: [],
      selectedRows: [],
      chooseData: 0,
      tableData: []
    }
  },
  watch: {
    $route(to, from) {
      if (to.params) {
        this.$nextTick(() => {
          // console.log(to.params)
          from.path === '/complaint/statistics' && this.$refs.searchData?.form.resetFields()
          this.$refs.searchData?.form.setFieldsValue(to.params)
          this.$refs.searchData?.search()
        })
      }
    }
  },
  created() {
    if (!this.$route.params || (this.$route.params instanceof Object && !Object.keys(this.$route.params)?.length)) {
      this.list()
    }
    this.initCode()
  },
  mounted: function () {
    if (this.$route.params) {
      const data = this.$route.params
          // console.log(data)
      // this.$refs.searchData?.form.resetFields()
      this.$refs.searchData?.form.setFieldsValue(data)
      this.$refs.searchData?.search()
    }
    this.$bus.$on('handleRefresh' + this.pathName, () => {
      this.list()
    })
  },
  beforeDestroy() {
    this.$bus.$off('handleRefresh' + this.pathName)
  },
  methods: {
    initCode() {
      getMaintenanceInfo().then((res) => {
        const data = []
        res.data.map((node) => {
          data.push({
            label: node.name,
            value: node.code
          })
        })
        this.searchArr[3].data = data
      })
    },
    list(callback) {
      this.loading = true
      let _this = this
      const searchData = {
        pageSize: callback ? _this.totalSize : _this.pageSize,
        pageNum: _this.pageNo,
        param: _this.queryParam
      }
      queryScoreList(searchData).then((res) => {
        if (res.code === 200) {
          if (callback) {
            if (res.data.list.length) {
              callback(res.data.list)
            }
            this.loading = false
            return
          }
          _this.tableData = res.data.list
          _this.totalSize = res.data.total
        }
        this.loading = false
      })
    },
    search(val) {
      if (val) {
        this.queryParam = val
      }
      this.list()
    },
    handlePageChange(pageNo, pageSize) {
      this.pageNo = pageNo
      this.list()
      this.selectedRowKeys = []
    },
    showSizeChange(pageNo, pageSize) {
      this.pageNo = 1
      this.pageSize = pageSize
      this.list()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      let _this = this
      _this.selectId = []
      selectedRows.map((item) => {
        _this.selectId.push(item.id)
      })
      this.chooseData = selectedRowKeys.length
      _this.selectedRowKeys = selectedRowKeys
      _this.selectedRows = selectedRows
    },

    handleEdit(record, state) {
      this.$router.push({
        name: 'AddMaintenanceSatisfaction',
        query: {
          id: (record && record.id) || '',
          state: state
        }
      })
    }
  }
}
</script>